﻿using System;

namespace MartinRL.ProjectEuler.Tests
{
    public class MaximumTotalFromTopToBottomOfATriangleCalculation
    {
        private readonly ushort[][] triangle;

        public MaximumTotalFromTopToBottomOfATriangleCalculation(ushort[][] triangle)
        {
            this.triangle = triangle;
        }

        public ushort Result
        {
            get
            {
                // As of now, this method violates the CQS principle (mutates triangle).
                // This is due to its usage, i.e. one shot at solving a specific puzzle.

                for (var i = triangle.Length - 1; i >= 1; i--)
                {
                    for (var j = 0; j < triangle[i].Length - 1; j++)
                    {
                        triangle[i - 1][j] += Math.Max(triangle[i][j], triangle[i][j + 1]);
                    }
                }

                return GetTopValue();
            }
        }

        private ushort GetTopValue()
        {
            return triangle[0][0];
        }
    }
}